package com.cardone.platform.authority.controller;

import java.util.List;
import java.util.Map;

import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.Validator;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.cardone.common.dto.PaginationDto;
import com.cardone.context.Constants;
import com.cardone.platform.authority.dto.UsGrBusinessAreaDto;
import com.cardone.platform.authority.dto.UsGroupOrgDto;
import com.cardone.platform.authority.dto.UsGroupRoleDto;
import com.cardone.platform.authority.dto.UsGroupUserDto;
import com.cardone.platform.authority.dto.UserGroupDto;
import com.cardone.platform.authority.service.AuthorityService;
import com.cardone.platform.authority.validator.UsGrBusinessAreaValidator;
import com.cardone.platform.authority.validator.UsGroupOrgValidator;
import com.cardone.platform.authority.validator.UsGroupRoleValidator;
import com.cardone.platform.authority.validator.UsGroupUserValidator;
import com.cardone.platform.authority.validator.UserGroupValidator;
import com.cardone.platform.authority.vo.UserGroupVo;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * 用户组
 * 
 * @author yaoht
 */
@Setter
@Slf4j
@RequestMapping(UserGroupController.Paths.ROOT)
@Controller
public class UserGroupController {
	/**
	 * models集合
	 * 
	 * @author yaoht
	 * 
	 */
	public enum models {
		/**
		 * 消息
		 */
		message,

		/**
		 * 用户组
		 */
		userGroup,

		/**
		 * 用户组与业务范围
		 */
		usGrBusinessArea,

		/**
		 * 用户组与组织
		 */
		usGroupOrg,

		/**
		 * 用户组与角色
		 */
		usGroupRole,

		/**
		 * 用户组与用户
		 */
		usGroupUser;
	}

	/**
	 * 路径集合
	 * 
	 * @author yaoht
	 * 
	 */
	public enum Paths {
		/**
		 * 用户组主界面
		 */
		index,

		/**
		 * 用户组与业务范围编辑视图路径
		 */
		US_GR_BUSINESS_AREAEdit("usGrBusinessArea/edit"),

		/**
		 * 用户组与业务范围主界面视图路径
		 */
		US_GR_BUSINESS_AREAIndex("usGrBusinessArea/index"),

		/**
		 * 用户组与业务范围编辑视图路径
		 */
		US_GR_BUSINESS_AREANew("usGrBusinessArea/new"),

		/**
		 * 用户组与业务范围查看编辑视图路径
		 */
		US_GR_BUSINESS_AREAView("usGrBusinessArea/view"),

		/**
		 * 用户组与组织编辑视图路径
		 */
		US_GROUP_ORGEdit("usGroupOrg/edit"),

		/**
		 * 用户组与组织主界面视图路径
		 */
		US_GROUP_ORGIndex("usGroupOrg/index"),

		/**
		 * 用户组与组织编辑视图路径
		 */
		US_GROUP_ORGNew("usGroupOrg/new"),

		/**
		 * 用户组与组织查看编辑视图路径
		 */
		US_GROUP_ORGView("usGroupOrg/view"),

		/**
		 * 用户组与角色编辑视图路径
		 */
		US_GROUP_ROLEEdit("usGroupRole/edit"),

		/**
		 * 用户组与角色主界面视图路径
		 */
		US_GROUP_ROLEIndex("usGroupRole/index"),

		/**
		 * 用户组与角色编辑视图路径
		 */
		US_GROUP_ROLENew("usGroupRole/new"),

		/**
		 * 用户组与角色查看编辑视图路径
		 */
		US_GROUP_ROLEView("usGroupRole/view"),

		/**
		 * 用户组与用户编辑视图路径
		 */
		US_GROUP_USEREdit("usGroupUser/edit"),

		/**
		 * 用户组与用户主界面视图路径
		 */
		US_GROUP_USERIndex("usGroupUser/index"),

		/**
		 * 用户组与用户编辑视图路径
		 */
		US_GROUP_USERNew("usGroupUser/new"),

		/**
		 * 用户组与用户查看编辑视图路径
		 */
		US_GROUP_USERView("usGroupUser/view"),

		/**
		 * 用户组编辑视图路径
		 */
		USER_GROUPEdit("userGroup/edit"),

		/**
		 * 用户组主界面视图路径
		 */
		USER_GROUPIndex("userGroup/index"),

		/**
		 * 用户组编辑视图路径
		 */
		USER_GROUPNew("userGroup/new"),

		/**
		 * 用户组查看编辑视图路径
		 */
		USER_GROUPView("userGroup/view");

		/**
		 * 用户组视图根目录
		 */
		public static final String ROOT = "/platform/authority/userGroup/";

		/**
		 * 路径
		 */
		private final String path;

		/**
		 * 构建
		 * 
		 */
		private Paths() {
			this.path = Paths.ROOT + this.name();
		}

		/**
		 * 构建
		 * 
		 * @param path
		 *            路径
		 */
		private Paths(final String path) {
			this.path = Paths.ROOT + path;
		}

		/**
		 * 获取
		 * 
		 * @return 路径
		 */
		public String path() {
			return this.path;
		}
	}

	/**
	 * 用户组服务
	 */
	@Qualifier(AuthorityService.BEAN_ID)
	@Autowired(required = false)
	private AuthorityService authorityService;

	/**
	 * 用户组验证
	 */
	@Qualifier(UserGroupValidator.BEAN_ID)
	@Autowired(required = false)
	public Validator userGroupValidator;

	/**
	 * 用户组与业务范围验证
	 */
	@Qualifier(UsGrBusinessAreaValidator.BEAN_ID)
	@Autowired(required = false)
	public Validator usGrBusinessAreaValidator;

	/**
	 * 用户组与组织验证
	 */
	@Qualifier(UsGroupOrgValidator.BEAN_ID)
	@Autowired(required = false)
	public Validator usGroupOrgValidator;

	/**
	 * 用户组与角色验证
	 */
	@Qualifier(UsGroupRoleValidator.BEAN_ID)
	@Autowired(required = false)
	public Validator usGroupRoleValidator;

	/**
	 * 用户组与用户验证
	 */
	@Qualifier(UsGroupUserValidator.BEAN_ID)
	@Autowired(required = false)
	public Validator usGroupUserValidator;

	/**
	 * 用户组：删除
	 * 
	 * @param userGroupVo
	 *            用户组视图对象
	 * @return 处理结果，json格式
	 */
	@RequestMapping(value = { "userGroup/delete.json" }, method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> userGroupDelete(final UserGroupVo userGroupVo) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.authorityService.getUserGroupService().deleteUserGroupByIds(userGroupVo.getUserGroup());
		} catch (final Exception e) {
			jsonMap.put(Constants.message.stringValue(), e.getMessage());

			if (UserGroupController.log.isDebugEnabled()) {
				UserGroupController.log.debug(e.getMessage(), e);
			}
		}

		return jsonMap;
	}

	/**
	 * 用户组：编辑
	 * 
	 * @param userGroupVo
	 *            用户组视图对象
	 * @param model
	 *            上下文映射
	 * @return 用户组编辑视图路径
	 */
	@RequestMapping(value = { "userGroup/edit.html" }, method = RequestMethod.GET)
	public String userGroupEdit(final UserGroupVo userGroupVo, final Model model) {
		final UserGroupDto userGroup = this.authorityService.getUserGroupService().findUserGroupById(userGroupVo.getUserGroup());

		model.addAttribute(UserGroupController.models.userGroup.name(), userGroup);

		return UserGroupController.Paths.USER_GROUPEdit.path();
	}

	/**
	 * 用户组：主界面
	 * 
	 * @return 用户组主界面视图路径
	 */
	@RequestMapping(value = { "userGroup/index.html" })
	public String userGroupIndex() {
		return UserGroupController.Paths.USER_GROUPIndex.path();
	}

	/**
	 * 用户组：主界面分页查询
	 * 
	 * @param userGroupVo
	 *            用户组视图对象
	 * @return 分页数据，json格式
	 */
	@RequestMapping(value = { "userGroup/index/pagination.json" })
	@ResponseBody
	public Map<String, Object> userGroupIndexPagination(final UserGroupVo userGroupVo) {
		final PaginationDto<UserGroupDto> userGroupPagination = this.authorityService.getUserGroupService().paginationUserGroupByCode(
		        userGroupVo.getUserGroup());

		final Map<String, Object> userGroupPaginationMap = Maps.newHashMap();

		userGroupPaginationMap.put(Constants.total.stringValue(), userGroupPagination.getTotalSize());
		userGroupPaginationMap.put(Constants.rows.stringValue(), userGroupPagination.getDataList());

		return userGroupPaginationMap;
	}

	/**
	 * 用户组：新增
	 * 
	 * @param userGroupVo
	 *            用户组视图对象
	 * @param model
	 *            上下文映射
	 * @return 用户组编辑视图路径
	 */
	@RequestMapping(value = { "userGroup/new.html" }, method = RequestMethod.GET)
	public String userGroupNew(final UserGroupVo userGroupVo, final Model model) {
		model.addAttribute(UserGroupController.models.userGroup.name(), userGroupVo.getUserGroup());

		return UserGroupController.Paths.USER_GROUPNew.path();
	}

	/**
	 * 用户组：保存
	 * 
	 * @param userGroupVo
	 *            用户组视图对象
	 * @param bindingResult
	 *            绑定对象
	 * @return 处理结果，json格式
	 */
	@RequestMapping(value = { "userGroup/save.txt" }, method = RequestMethod.POST)
	@ResponseBody
	public String userGroupSave(final UserGroupVo userGroupVo, BindingResult bindingResult) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.userGroupValidator.validate(userGroupVo.getUserGroup(), bindingResult);

			if (bindingResult.hasErrors()) {
				List<String> messageList = Lists.newArrayList();

				for (final ObjectError objectError : bindingResult.getAllErrors()) {
					messageList.add(objectError.getDefaultMessage());
				}

				String message = StringUtils.join(messageList, org.apache.commons.lang3.StringUtils.EMPTY);

				throw new Exception(message);
			}

			this.authorityService.getUserGroupService().saveUserGroupByIdOrCode(userGroupVo.getUserGroup());
		} catch (final Exception e) {
			jsonMap.put(Constants.message.stringValue(), e.getMessage());

			if (UserGroupController.log.isDebugEnabled()) {
				UserGroupController.log.debug(e.getMessage(), e);
			}
		}

		return JSON.toJSONString(jsonMap);
	}

	/**
	 * 用户组：更新
	 * 
	 * @param userGroupVo
	 *            用户组视图对象
	 * @return 处理结果，json格式
	 */
	@RequestMapping(value = { "userGroup/update.json" }, method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> userGroupUpdate(final UserGroupVo userGroupVo) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.authorityService.getUserGroupService().updateUserGroupByIds(userGroupVo.getUserGroup());
		} catch (final Exception e) {
			jsonMap.put(Constants.message.stringValue(), e.getMessage());

			if (UserGroupController.log.isDebugEnabled()) {
				UserGroupController.log.debug(e.getMessage(), e);
			}
		}

		return jsonMap;
	}

	/**
	 * 用户组：验证
	 * 
	 * @param userGroupVo
	 *            用户组视图对象
	 * @return 验证结果
	 */
	@RequestMapping(value = { "userGroup/validationByCode.json" })
	@ResponseBody
	public Boolean userGroupValidationByCode(final UserGroupVo userGroupVo) {
		try {
			final int count = this.authorityService.getUserGroupService().readUserGroupByCodeNqId(userGroupVo.getUserGroup());

			return (count < 1);
		} catch (final Exception e) {
			if (UserGroupController.log.isDebugEnabled()) {
				UserGroupController.log.debug(e.getMessage(), e);
			}
		}

		return false;
	}

	/**
	 * 用户组：查看
	 * 
	 * @param userGroupVo
	 *            用户组视图对象
	 * @param model
	 *            上下文映射
	 * @return 用户组查看编辑视图路径
	 */
	@RequestMapping(value = { "userGroup/view.html" }, method = RequestMethod.GET)
	public String userGroupView(final UserGroupVo userGroupVo, final Model model) {
		final UserGroupDto userGroup = this.authorityService.getUserGroupService().findUserGroupById(userGroupVo.getUserGroup());

		model.addAttribute(UserGroupController.models.userGroup.name(), userGroup);

		return UserGroupController.Paths.USER_GROUPView.path();
	}

	/**
	 * 用户组与业务范围：删除
	 * 
	 * @param userGroupVo
	 *            用户组与业务范围视图对象
	 * @return 处理结果，json格式
	 */
	@RequestMapping(value = { "usGrBusinessArea/delete.json" }, method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> usGrBusinessAreaDelete(final UserGroupVo userGroupVo) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.authorityService.getUserGroupService().deleteUsGrBusinessAreaByIds(userGroupVo.getUsGrBusinessArea());
		} catch (final Exception e) {
			jsonMap.put(Constants.message.stringValue(), e.getMessage());

			if (UserGroupController.log.isDebugEnabled()) {
				UserGroupController.log.debug(e.getMessage(), e);
			}
		}

		return jsonMap;
	}

	/**
	 * 用户组与业务范围：编辑
	 * 
	 * @param userGroupVo
	 *            用户组与业务范围视图对象
	 * @param model
	 *            上下文映射
	 * @return 用户组与业务范围编辑视图路径
	 */
	@RequestMapping(value = { "usGrBusinessArea/edit.html" }, method = RequestMethod.GET)
	public String usGrBusinessAreaEdit(final UserGroupVo userGroupVo, final Model model) {
		final UsGrBusinessAreaDto usGrBusinessArea = this.authorityService.getUserGroupService().findUsGrBusinessAreaById(
		        userGroupVo.getUsGrBusinessArea());

		model.addAttribute(UserGroupController.models.usGrBusinessArea.name(), usGrBusinessArea);

		return UserGroupController.Paths.US_GR_BUSINESS_AREAEdit.path();
	}

	/**
	 * 用户组与业务范围：主界面
	 * 
	 * @return 用户组与业务范围主界面视图路径
	 */
	@RequestMapping(value = { "usGrBusinessArea/index.html" })
	public String usGrBusinessAreaIndex() {
		return UserGroupController.Paths.US_GR_BUSINESS_AREAIndex.path();
	}

	/**
	 * 用户组与业务范围：主界面分页查询
	 * 
	 * @param userGroupVo
	 *            用户组与业务范围视图对象
	 * @return 分页数据，json格式
	 */
	@RequestMapping(value = { "usGrBusinessArea/index/pagination.json" })
	@ResponseBody
	public Map<String, Object> usGrBusinessAreaIndexPagination(final UserGroupVo userGroupVo) {
		final PaginationDto<UsGrBusinessAreaDto> usGrBusinessAreaPagination = this.authorityService.getUserGroupService()
		        .paginationUsGrBusinessAreaByCode(userGroupVo.getUsGrBusinessArea());

		final Map<String, Object> usGrBusinessAreaPaginationMap = Maps.newHashMap();

		usGrBusinessAreaPaginationMap.put(Constants.total.stringValue(), usGrBusinessAreaPagination.getTotalSize());
		usGrBusinessAreaPaginationMap.put(Constants.rows.stringValue(), usGrBusinessAreaPagination.getDataList());

		return usGrBusinessAreaPaginationMap;
	}

	/**
	 * 用户组与业务范围：新增
	 * 
	 * @param userGroupVo
	 *            用户组与业务范围视图对象
	 * @param model
	 *            上下文映射
	 * @return 用户组与业务范围编辑视图路径
	 */
	@RequestMapping(value = { "usGrBusinessArea/new.html" }, method = RequestMethod.GET)
	public String usGrBusinessAreaNew(final UserGroupVo userGroupVo, final Model model) {
		model.addAttribute(UserGroupController.models.usGrBusinessArea.name(), userGroupVo.getUsGrBusinessArea());

		return UserGroupController.Paths.US_GR_BUSINESS_AREANew.path();
	}

	/**
	 * 用户组与业务范围：保存
	 * 
	 * @param userGroupVo
	 *            用户组与业务范围视图对象
	 * @param bindingResult
	 *            绑定对象
	 * @return 处理结果，json格式
	 */
	@RequestMapping(value = { "usGrBusinessArea/save.txt" }, method = RequestMethod.POST)
	@ResponseBody
	public String usGrBusinessAreaSave(final UserGroupVo userGroupVo, BindingResult bindingResult) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.usGrBusinessAreaValidator.validate(userGroupVo.getUsGrBusinessArea(), bindingResult);

			if (bindingResult.hasErrors()) {
				List<String> messageList = Lists.newArrayList();

				for (final ObjectError objectError : bindingResult.getAllErrors()) {
					messageList.add(objectError.getDefaultMessage());
				}

				String message = StringUtils.join(messageList, org.apache.commons.lang3.StringUtils.EMPTY);

				throw new Exception(message);
			}

			this.authorityService.getUserGroupService().saveUsGrBusinessAreaByIdOrCode(userGroupVo.getUsGrBusinessArea());
		} catch (final Exception e) {
			jsonMap.put(Constants.message.stringValue(), e.getMessage());

			if (UserGroupController.log.isDebugEnabled()) {
				UserGroupController.log.debug(e.getMessage(), e);
			}
		}

		return JSON.toJSONString(jsonMap);
	}

	/**
	 * 用户组与业务范围：更新
	 * 
	 * @param userGroupVo
	 *            用户组与业务范围视图对象
	 * @return 处理结果，json格式
	 */
	@RequestMapping(value = { "usGrBusinessArea/update.json" }, method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> usGrBusinessAreaUpdate(final UserGroupVo userGroupVo) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.authorityService.getUserGroupService().updateUsGrBusinessAreaByIds(userGroupVo.getUsGrBusinessArea());
		} catch (final Exception e) {
			jsonMap.put(Constants.message.stringValue(), e.getMessage());

			if (UserGroupController.log.isDebugEnabled()) {
				UserGroupController.log.debug(e.getMessage(), e);
			}
		}

		return jsonMap;
	}

	/**
	 * 用户组与业务范围：验证
	 * 
	 * @param userGroupVo
	 *            用户组与业务范围视图对象
	 * @return 验证结果
	 */
	@RequestMapping(value = { "usGrBusinessArea/validationByCode.json" })
	@ResponseBody
	public Boolean usGrBusinessAreaValidationByCode(final UserGroupVo userGroupVo) {
		try {
			final int count = this.authorityService.getUserGroupService().readUsGrBusinessAreaByCodeNqId(
			        userGroupVo.getUsGrBusinessArea());

			return (count < 1);
		} catch (final Exception e) {
			if (UserGroupController.log.isDebugEnabled()) {
				UserGroupController.log.debug(e.getMessage(), e);
			}
		}

		return false;
	}

	/**
	 * 用户组与业务范围：查看
	 * 
	 * @param userGroupVo
	 *            用户组与业务范围视图对象
	 * @param model
	 *            上下文映射
	 * @return 用户组与业务范围查看编辑视图路径
	 */
	@RequestMapping(value = { "usGrBusinessArea/view.html" }, method = RequestMethod.GET)
	public String usGrBusinessAreaView(final UserGroupVo userGroupVo, final Model model) {
		final UsGrBusinessAreaDto usGrBusinessArea = this.authorityService.getUserGroupService().findUsGrBusinessAreaById(
		        userGroupVo.getUsGrBusinessArea());

		model.addAttribute(UserGroupController.models.usGrBusinessArea.name(), usGrBusinessArea);

		return UserGroupController.Paths.US_GR_BUSINESS_AREAView.path();
	}

	/**
	 * 用户组与组织：删除
	 * 
	 * @param userGroupVo
	 *            用户组与组织视图对象
	 * @return 处理结果，json格式
	 */
	@RequestMapping(value = { "usGroupOrg/delete.json" }, method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> usGroupOrgDelete(final UserGroupVo userGroupVo) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.authorityService.getUserGroupService().deleteUsGroupOrgByIds(userGroupVo.getUsGroupOrg());
		} catch (final Exception e) {
			jsonMap.put(Constants.message.stringValue(), e.getMessage());

			if (UserGroupController.log.isDebugEnabled()) {
				UserGroupController.log.debug(e.getMessage(), e);
			}
		}

		return jsonMap;
	}

	/**
	 * 用户组与组织：编辑
	 * 
	 * @param userGroupVo
	 *            用户组与组织视图对象
	 * @param model
	 *            上下文映射
	 * @return 用户组与组织编辑视图路径
	 */
	@RequestMapping(value = { "usGroupOrg/edit.html" }, method = RequestMethod.GET)
	public String usGroupOrgEdit(final UserGroupVo userGroupVo, final Model model) {
		final UsGroupOrgDto usGroupOrg = this.authorityService.getUserGroupService().findUsGroupOrgById(userGroupVo.getUsGroupOrg());

		model.addAttribute(UserGroupController.models.usGroupOrg.name(), usGroupOrg);

		return UserGroupController.Paths.US_GROUP_ORGEdit.path();
	}

	/**
	 * 用户组与组织：主界面
	 * 
	 * @return 用户组与组织主界面视图路径
	 */
	@RequestMapping(value = { "usGroupOrg/index.html" })
	public String usGroupOrgIndex() {
		return UserGroupController.Paths.US_GROUP_ORGIndex.path();
	}

	/**
	 * 用户组与组织：主界面分页查询
	 * 
	 * @param userGroupVo
	 *            用户组与组织视图对象
	 * @return 分页数据，json格式
	 */
	@RequestMapping(value = { "usGroupOrg/index/pagination.json" })
	@ResponseBody
	public Map<String, Object> usGroupOrgIndexPagination(final UserGroupVo userGroupVo) {
		final PaginationDto<UsGroupOrgDto> usGroupOrgPagination = this.authorityService.getUserGroupService()
		        .paginationUsGroupOrgByCode(userGroupVo.getUsGroupOrg());

		final Map<String, Object> usGroupOrgPaginationMap = Maps.newHashMap();

		usGroupOrgPaginationMap.put(Constants.total.stringValue(), usGroupOrgPagination.getTotalSize());
		usGroupOrgPaginationMap.put(Constants.rows.stringValue(), usGroupOrgPagination.getDataList());

		return usGroupOrgPaginationMap;
	}

	/**
	 * 用户组与组织：新增
	 * 
	 * @param userGroupVo
	 *            用户组与组织视图对象
	 * @param model
	 *            上下文映射
	 * @return 用户组与组织编辑视图路径
	 */
	@RequestMapping(value = { "usGroupOrg/new.html" }, method = RequestMethod.GET)
	public String usGroupOrgNew(final UserGroupVo userGroupVo, final Model model) {
		model.addAttribute(UserGroupController.models.usGroupOrg.name(), userGroupVo.getUsGroupOrg());

		return UserGroupController.Paths.US_GROUP_ORGNew.path();
	}

	/**
	 * 用户组与组织：保存
	 * 
	 * @param userGroupVo
	 *            用户组与组织视图对象
	 * @param bindingResult
	 *            绑定对象
	 * @return 处理结果，json格式
	 */
	@RequestMapping(value = { "usGroupOrg/save.txt" }, method = RequestMethod.POST)
	@ResponseBody
	public String usGroupOrgSave(final UserGroupVo userGroupVo, BindingResult bindingResult) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.usGroupOrgValidator.validate(userGroupVo.getUsGroupOrg(), bindingResult);

			if (bindingResult.hasErrors()) {
				List<String> messageList = Lists.newArrayList();

				for (final ObjectError objectError : bindingResult.getAllErrors()) {
					messageList.add(objectError.getDefaultMessage());
				}

				String message = StringUtils.join(messageList, org.apache.commons.lang3.StringUtils.EMPTY);

				throw new Exception(message);
			}

			this.authorityService.getUserGroupService().saveUsGroupOrgByIdOrCode(userGroupVo.getUsGroupOrg());
		} catch (final Exception e) {
			jsonMap.put(Constants.message.stringValue(), e.getMessage());

			if (UserGroupController.log.isDebugEnabled()) {
				UserGroupController.log.debug(e.getMessage(), e);
			}
		}

		return JSON.toJSONString(jsonMap);
	}

	/**
	 * 用户组与组织：更新
	 * 
	 * @param userGroupVo
	 *            用户组与组织视图对象
	 * @return 处理结果，json格式
	 */
	@RequestMapping(value = { "usGroupOrg/update.json" }, method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> usGroupOrgUpdate(final UserGroupVo userGroupVo) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.authorityService.getUserGroupService().updateUsGroupOrgByIds(userGroupVo.getUsGroupOrg());
		} catch (final Exception e) {
			jsonMap.put(Constants.message.stringValue(), e.getMessage());

			if (UserGroupController.log.isDebugEnabled()) {
				UserGroupController.log.debug(e.getMessage(), e);
			}
		}

		return jsonMap;
	}

	/**
	 * 用户组与组织：验证
	 * 
	 * @param userGroupVo
	 *            用户组与组织视图对象
	 * @return 验证结果
	 */
	@RequestMapping(value = { "usGroupOrg/validationByCode.json" })
	@ResponseBody
	public Boolean usGroupOrgValidationByCode(final UserGroupVo userGroupVo) {
		try {
			final int count = this.authorityService.getUserGroupService().readUsGroupOrgByCodeNqId(userGroupVo.getUsGroupOrg());

			return (count < 1);
		} catch (final Exception e) {
			if (UserGroupController.log.isDebugEnabled()) {
				UserGroupController.log.debug(e.getMessage(), e);
			}
		}

		return false;
	}

	/**
	 * 用户组与组织：查看
	 * 
	 * @param userGroupVo
	 *            用户组与组织视图对象
	 * @param model
	 *            上下文映射
	 * @return 用户组与组织查看编辑视图路径
	 */
	@RequestMapping(value = { "usGroupOrg/view.html" }, method = RequestMethod.GET)
	public String usGroupOrgView(final UserGroupVo userGroupVo, final Model model) {
		final UsGroupOrgDto usGroupOrg = this.authorityService.getUserGroupService().findUsGroupOrgById(userGroupVo.getUsGroupOrg());

		model.addAttribute(UserGroupController.models.usGroupOrg.name(), usGroupOrg);

		return UserGroupController.Paths.US_GROUP_ORGView.path();
	}

	/**
	 * 用户组与角色：删除
	 * 
	 * @param userGroupVo
	 *            用户组与角色视图对象
	 * @return 处理结果，json格式
	 */
	@RequestMapping(value = { "usGroupRole/delete.json" }, method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> usGroupRoleDelete(final UserGroupVo userGroupVo) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.authorityService.getUserGroupService().deleteUsGroupRoleByIds(userGroupVo.getUsGroupRole());
		} catch (final Exception e) {
			jsonMap.put(Constants.message.stringValue(), e.getMessage());

			if (UserGroupController.log.isDebugEnabled()) {
				UserGroupController.log.debug(e.getMessage(), e);
			}
		}

		return jsonMap;
	}

	/**
	 * 用户组与角色：编辑
	 * 
	 * @param userGroupVo
	 *            用户组与角色视图对象
	 * @param model
	 *            上下文映射
	 * @return 用户组与角色编辑视图路径
	 */
	@RequestMapping(value = { "usGroupRole/edit.html" }, method = RequestMethod.GET)
	public String usGroupRoleEdit(final UserGroupVo userGroupVo, final Model model) {
		final UsGroupRoleDto usGroupRole = this.authorityService.getUserGroupService()
		        .findUsGroupRoleById(userGroupVo.getUsGroupRole());

		model.addAttribute(UserGroupController.models.usGroupRole.name(), usGroupRole);

		return UserGroupController.Paths.US_GROUP_ROLEEdit.path();
	}

	/**
	 * 用户组与角色：主界面
	 * 
	 * @return 用户组与角色主界面视图路径
	 */
	@RequestMapping(value = { "usGroupRole/index.html" })
	public String usGroupRoleIndex() {
		return UserGroupController.Paths.US_GROUP_ROLEIndex.path();
	}

	/**
	 * 用户组与角色：主界面分页查询
	 * 
	 * @param userGroupVo
	 *            用户组与角色视图对象
	 * @return 分页数据，json格式
	 */
	@RequestMapping(value = { "usGroupRole/index/pagination.json" })
	@ResponseBody
	public Map<String, Object> usGroupRoleIndexPagination(final UserGroupVo userGroupVo) {
		final PaginationDto<UsGroupRoleDto> usGroupRolePagination = this.authorityService.getUserGroupService()
		        .paginationUsGroupRoleByCode(userGroupVo.getUsGroupRole());

		final Map<String, Object> usGroupRolePaginationMap = Maps.newHashMap();

		usGroupRolePaginationMap.put(Constants.total.stringValue(), usGroupRolePagination.getTotalSize());
		usGroupRolePaginationMap.put(Constants.rows.stringValue(), usGroupRolePagination.getDataList());

		return usGroupRolePaginationMap;
	}

	/**
	 * 用户组与角色：新增
	 * 
	 * @param userGroupVo
	 *            用户组与角色视图对象
	 * @param model
	 *            上下文映射
	 * @return 用户组与角色编辑视图路径
	 */
	@RequestMapping(value = { "usGroupRole/new.html" }, method = RequestMethod.GET)
	public String usGroupRoleNew(final UserGroupVo userGroupVo, final Model model) {
		model.addAttribute(UserGroupController.models.usGroupRole.name(), userGroupVo.getUsGroupRole());

		return UserGroupController.Paths.US_GROUP_ROLENew.path();
	}

	/**
	 * 用户组与角色：保存
	 * 
	 * @param userGroupVo
	 *            用户组与角色视图对象
	 * @param bindingResult
	 *            绑定对象
	 * @return 处理结果，json格式
	 */
	@RequestMapping(value = { "usGroupRole/save.txt" }, method = RequestMethod.POST)
	@ResponseBody
	public String usGroupRoleSave(final UserGroupVo userGroupVo, BindingResult bindingResult) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.usGroupRoleValidator.validate(userGroupVo.getUsGroupRole(), bindingResult);

			if (bindingResult.hasErrors()) {
				List<String> messageList = Lists.newArrayList();

				for (final ObjectError objectError : bindingResult.getAllErrors()) {
					messageList.add(objectError.getDefaultMessage());
				}

				String message = StringUtils.join(messageList, org.apache.commons.lang3.StringUtils.EMPTY);

				throw new Exception(message);
			}

			this.authorityService.getUserGroupService().saveUsGroupRoleByIdOrCode(userGroupVo.getUsGroupRole());
		} catch (final Exception e) {
			jsonMap.put(Constants.message.stringValue(), e.getMessage());

			if (UserGroupController.log.isDebugEnabled()) {
				UserGroupController.log.debug(e.getMessage(), e);
			}
		}

		return JSON.toJSONString(jsonMap);
	}

	/**
	 * 用户组与角色：更新
	 * 
	 * @param userGroupVo
	 *            用户组与角色视图对象
	 * @return 处理结果，json格式
	 */
	@RequestMapping(value = { "usGroupRole/update.json" }, method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> usGroupRoleUpdate(final UserGroupVo userGroupVo) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.authorityService.getUserGroupService().updateUsGroupRoleByIds(userGroupVo.getUsGroupRole());
		} catch (final Exception e) {
			jsonMap.put(Constants.message.stringValue(), e.getMessage());

			if (UserGroupController.log.isDebugEnabled()) {
				UserGroupController.log.debug(e.getMessage(), e);
			}
		}

		return jsonMap;
	}

	/**
	 * 用户组与角色：验证
	 * 
	 * @param userGroupVo
	 *            用户组与角色视图对象
	 * @return 验证结果
	 */
	@RequestMapping(value = { "usGroupRole/validationByCode.json" })
	@ResponseBody
	public Boolean usGroupRoleValidationByCode(final UserGroupVo userGroupVo) {
		try {
			final int count = this.authorityService.getUserGroupService().readUsGroupRoleByCodeNqId(userGroupVo.getUsGroupRole());

			return (count < 1);
		} catch (final Exception e) {
			if (UserGroupController.log.isDebugEnabled()) {
				UserGroupController.log.debug(e.getMessage(), e);
			}
		}

		return false;
	}

	/**
	 * 用户组与角色：查看
	 * 
	 * @param userGroupVo
	 *            用户组与角色视图对象
	 * @param model
	 *            上下文映射
	 * @return 用户组与角色查看编辑视图路径
	 */
	@RequestMapping(value = { "usGroupRole/view.html" }, method = RequestMethod.GET)
	public String usGroupRoleView(final UserGroupVo userGroupVo, final Model model) {
		final UsGroupRoleDto usGroupRole = this.authorityService.getUserGroupService()
		        .findUsGroupRoleById(userGroupVo.getUsGroupRole());

		model.addAttribute(UserGroupController.models.usGroupRole.name(), usGroupRole);

		return UserGroupController.Paths.US_GROUP_ROLEView.path();
	}

	/**
	 * 用户组与用户：删除
	 * 
	 * @param userGroupVo
	 *            用户组与用户视图对象
	 * @return 处理结果，json格式
	 */
	@RequestMapping(value = { "usGroupUser/delete.json" }, method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> usGroupUserDelete(final UserGroupVo userGroupVo) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.authorityService.getUserGroupService().deleteUsGroupUserByIds(userGroupVo.getUsGroupUser());
		} catch (final Exception e) {
			jsonMap.put(Constants.message.stringValue(), e.getMessage());

			if (UserGroupController.log.isDebugEnabled()) {
				UserGroupController.log.debug(e.getMessage(), e);
			}
		}

		return jsonMap;
	}

	/**
	 * 用户组与用户：编辑
	 * 
	 * @param userGroupVo
	 *            用户组与用户视图对象
	 * @param model
	 *            上下文映射
	 * @return 用户组与用户编辑视图路径
	 */
	@RequestMapping(value = { "usGroupUser/edit.html" }, method = RequestMethod.GET)
	public String usGroupUserEdit(final UserGroupVo userGroupVo, final Model model) {
		final UsGroupUserDto usGroupUser = this.authorityService.getUserGroupService()
		        .findUsGroupUserById(userGroupVo.getUsGroupUser());

		model.addAttribute(UserGroupController.models.usGroupUser.name(), usGroupUser);

		return UserGroupController.Paths.US_GROUP_USEREdit.path();
	}

	/**
	 * 用户组与用户：主界面
	 * 
	 * @return 用户组与用户主界面视图路径
	 */
	@RequestMapping(value = { "usGroupUser/index.html" })
	public String usGroupUserIndex() {
		return UserGroupController.Paths.US_GROUP_USERIndex.path();
	}

	/**
	 * 用户组与用户：主界面分页查询
	 * 
	 * @param userGroupVo
	 *            用户组与用户视图对象
	 * @return 分页数据，json格式
	 */
	@RequestMapping(value = { "usGroupUser/index/pagination.json" })
	@ResponseBody
	public Map<String, Object> usGroupUserIndexPagination(final UserGroupVo userGroupVo) {
		final PaginationDto<UsGroupUserDto> usGroupUserPagination = this.authorityService.getUserGroupService()
		        .paginationUsGroupUserByCode(userGroupVo.getUsGroupUser());

		final Map<String, Object> usGroupUserPaginationMap = Maps.newHashMap();

		usGroupUserPaginationMap.put(Constants.total.stringValue(), usGroupUserPagination.getTotalSize());
		usGroupUserPaginationMap.put(Constants.rows.stringValue(), usGroupUserPagination.getDataList());

		return usGroupUserPaginationMap;
	}

	/**
	 * 用户组与用户：新增
	 * 
	 * @param userGroupVo
	 *            用户组与用户视图对象
	 * @param model
	 *            上下文映射
	 * @return 用户组与用户编辑视图路径
	 */
	@RequestMapping(value = { "usGroupUser/new.html" }, method = RequestMethod.GET)
	public String usGroupUserNew(final UserGroupVo userGroupVo, final Model model) {
		model.addAttribute(UserGroupController.models.usGroupUser.name(), userGroupVo.getUsGroupUser());

		return UserGroupController.Paths.US_GROUP_USERNew.path();
	}

	/**
	 * 用户组与用户：保存
	 * 
	 * @param userGroupVo
	 *            用户组与用户视图对象
	 * @param bindingResult
	 *            绑定对象
	 * @return 处理结果，json格式
	 */
	@RequestMapping(value = { "usGroupUser/save.txt" }, method = RequestMethod.POST)
	@ResponseBody
	public String usGroupUserSave(final UserGroupVo userGroupVo, BindingResult bindingResult) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.usGroupUserValidator.validate(userGroupVo.getUsGroupUser(), bindingResult);

			if (bindingResult.hasErrors()) {
				List<String> messageList = Lists.newArrayList();

				for (final ObjectError objectError : bindingResult.getAllErrors()) {
					messageList.add(objectError.getDefaultMessage());
				}

				String message = StringUtils.join(messageList, org.apache.commons.lang3.StringUtils.EMPTY);

				throw new Exception(message);
			}

			this.authorityService.getUserGroupService().saveUsGroupUserByIdOrCode(userGroupVo.getUsGroupUser());
		} catch (final Exception e) {
			jsonMap.put(Constants.message.stringValue(), e.getMessage());

			if (UserGroupController.log.isDebugEnabled()) {
				UserGroupController.log.debug(e.getMessage(), e);
			}
		}

		return JSON.toJSONString(jsonMap);
	}

	/**
	 * 用户组与用户：更新
	 * 
	 * @param userGroupVo
	 *            用户组与用户视图对象
	 * @return 处理结果，json格式
	 */
	@RequestMapping(value = { "usGroupUser/update.json" }, method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> usGroupUserUpdate(final UserGroupVo userGroupVo) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.authorityService.getUserGroupService().updateUsGroupUserByIds(userGroupVo.getUsGroupUser());
		} catch (final Exception e) {
			jsonMap.put(Constants.message.stringValue(), e.getMessage());

			if (UserGroupController.log.isDebugEnabled()) {
				UserGroupController.log.debug(e.getMessage(), e);
			}
		}

		return jsonMap;
	}

	/**
	 * 用户组与用户：验证
	 * 
	 * @param userGroupVo
	 *            用户组与用户视图对象
	 * @return 验证结果
	 */
	@RequestMapping(value = { "usGroupUser/validationByCode.json" })
	@ResponseBody
	public Boolean usGroupUserValidationByCode(final UserGroupVo userGroupVo) {
		try {
			final int count = this.authorityService.getUserGroupService().readUsGroupUserByCodeNqId(userGroupVo.getUsGroupUser());

			return (count < 1);
		} catch (final Exception e) {
			if (UserGroupController.log.isDebugEnabled()) {
				UserGroupController.log.debug(e.getMessage(), e);
			}
		}

		return false;
	}

	/**
	 * 用户组与用户：查看
	 * 
	 * @param userGroupVo
	 *            用户组与用户视图对象
	 * @param model
	 *            上下文映射
	 * @return 用户组与用户查看编辑视图路径
	 */
	@RequestMapping(value = { "usGroupUser/view.html" }, method = RequestMethod.GET)
	public String usGroupUserView(final UserGroupVo userGroupVo, final Model model) {
		final UsGroupUserDto usGroupUser = this.authorityService.getUserGroupService()
		        .findUsGroupUserById(userGroupVo.getUsGroupUser());

		model.addAttribute(UserGroupController.models.usGroupUser.name(), usGroupUser);

		return UserGroupController.Paths.US_GROUP_USERView.path();
	}
}
